<?php
require_once('../exceptions/DatabaseException.php');

class Delete extends Query
{
	protected $sourceTables;
	protected $destinationTables;
	protected $conditions;
	protected $leftJoins;
	protected $innerJoins;
	protected $rightJoins;
	protected $leftOuterJoins;
	protected $outerJoins;
	protected $rightOuterJoins;
	protected $crossJoins;
	
	public function __construct($_destinationTables)
	{
		$this->destinationTables=$_destinationTables;
		$this->sourceTables=array();
		$this->conditions=array();
		$this->leftJoins=array();
		$this->innerJoins=array();
		$this->rightJoins=array();
		$this->leftOuterJoins=array();
		$this->outerJoins=array();
		$this->rightOuterJoins=array();
		$this->crossJoins=array();
	}
	
	public function from($_sourceTables)
	{
		$this->sourceTables=array_merge($this->sourceTables,$_sourceTables);
	}
	
	public function where($_conditions)
	{
		$this->conditions=array_merge($this->conditions,$_conditions);
	}
	
	public function leftJoin($_leftJoins)
	{
		$this->leftJoins=array_merge($this->leftJoins,$_leftJoins);
	}
	
	public function innerJoin($_innerJoins)
	{
		$this->innerJoins=array_merge($this->innerJoins,$_innerJoins);
	}
	
	public function rightJoin($_rightJoins)
	{
		$this->rightJoins=array_merge($this->rightJoins,$_rightJoins);
	}
	
	public function leftOuterJoin($_leftOuterJoins)
	{
		$this->leftOuterJoins=array_merge($this->leftOuterJoins,$_leftOuterJoins);
	}
	
	public function outerJoin($_outerJoins)
	{
		$this->outerJoins=array_merge($this->outerJoins,$_outerJoins);
	}
	
	public function rightOuterJoin($_rightOuterJoins)
	{
		$this->rightOuterJoins=array_merge($this->rightOuterJoins,$_rightOuterJoins);
	}
	
	public function crossJoin($_crossJoins)
	{
		$this->crossJoins=array_merge($this->crossJoins,$_crossJoins);
	}
	
	public function prepare()
	{
		if(count($this->destinationTables)<=0) {
			throw new DatabaseException(DatabaseException::ERROR_NO_TABLE_EXISTS_IN_DELETE_QUERY,DatabaseException::NO_NO_TABLE_EXISTS_IN_DELETE_QUERY);
		}
		$this->content="DELETE ".$this->destinationTables[0];
		for($i=1;$i<count($this->destinationTables);$i++)
			$this->content.=", ".$this->destinationTables[$i];
		if(count($this->sourceTables)<=0) {
			$this->sourceTables=$this->destinationTables;
		}
		$this->content.=" FROM ".$this->sourceTables[0];
		for($i=1;$i<count($this->sourceTables);$i++)
			$this->content.=", ".$this->sourceTables[$i];
		
		for($i=0;$i<count($this->leftJoins);$i++)
			$this->content.=" LEFT JOIN ".$this->leftJoins[$i]->getContent();
		for($i=0;$i<count($this->innerJoins);$i++)
			$this->content.=" INNER JOIN ".$this->innerJoins[$i]->getContent();
		for($i=0;$i<count($this->rightJoins);$i++)
			$this->content.=" RIGHT JOIN ".$this->rightJoins[$i]->getContent();
		for($i=0;$i<count($this->leftOuterJoins);$i++)
			$this->content.=" LEFT OUTER JOIN ".$this->leftOuterJoins[$i]->getContent();
		for($i=0;$i<count($this->outerJoins);$i++)
			$this->content.=" OUTER JOIN ".$this->outerJoins[$i]->getContent();
		for($i=0;$i<count($this->rightOuterJoins);$i++)
			$this->content.=" RIGHT OUTER JOIN ".$this->rightOuterJoins[$i]->getContent();
		for($i=0;$i<count($this->crossJoins);$i++)
			$this->content.=" CROSS JOIN ".$this->crossJoins[$i]->getContent();
			
		if(count($this->conditions)>0) {
			$this->content.=" WHERE ".$this->escape($this->conditions[0]);
			for($i=0;$i<count($this->content);$i++)
				$this->content.=" AND ".$this->escape($this->conditions[$i]);
		}
	}
	
}
?>